using System;
using System.Windows.Forms;
using NUnit.Framework;
using ShadeTree.Binding.Elements;

namespace ShadeTree.Binding.Tests.Elements
{
    [TestFixture]
    public class CheckboxElementTester
    {
        #region Setup/Teardown

        [SetUp]
        public void SetUp()
        {
            _box = new CheckBox();
            _box.Text = "something";
            Type type = typeof (CheckboxTarget);
            _property = SingleProperty.Build<CheckboxTarget>(x => x.BoolValue);
            _element = new CheckboxElement(_property, _box);
        }

        #endregion

        private CheckboxElement _element;
        private CheckBox _box;
        private Accessor _property;

        [Test]
        public void ApplyChanges()
        {
            var target = new CheckboxTarget();
            _element.Bind(target);

            _box.Checked = false;
            _element.ApplyChanges();
            Assert.AreEqual(_box.Checked, target.BoolValue);

            _box.Checked = true;
            _element.ApplyChanges();
            Assert.AreEqual(_box.Checked, target.BoolValue);

            _box.Checked = false;
            _element.ApplyChanges();
            Assert.AreEqual(_box.Checked, target.BoolValue);

            _box.Checked = true;
            _element.ApplyChanges();
            Assert.AreEqual(_box.Checked, target.BoolValue);
        }

        [Test]
        public void BindValue()
        {
            var target = new CheckboxTarget();

            target.BoolValue = true;
            _element.Bind(target);
            Assert.IsTrue(_box.Checked);

            target.BoolValue = false;
            _element.Bind(target);
            Assert.IsFalse(_box.Checked);

            target.BoolValue = true;
            _element.Bind(target);
            Assert.IsTrue(_box.Checked);

            target.BoolValue = false;
            _element.Bind(target);
            Assert.IsFalse(_box.Checked);
        }

        [Test]
        public void FieldName()
        {
            Assert.AreEqual(_property.FieldName, _element.FieldName);
        }

        [Test]
        public void LabelText()
        {
            Assert.AreEqual(_box.Text, _element.LabelText);
        }

        [Test]
        public void Reset()
        {
            var target = new CheckboxTarget();
            target.BoolValue = false;

            _element.Bind(target);
            Assert.IsFalse(_box.Checked);
            _box.Checked = true;

            _element.Reset();

            Assert.IsFalse(_box.Checked);
        }
    }

    public class CheckboxTarget
    {
        public bool BoolValue { get; set; }
    }
}